package dao

import (
	"fmt"
	"github.com/streadway/amqp"
	"go.uber.org/zap"
	"mall-portalv2/global"
)

func (d *Dao) ReadRabbitMqMessage(key string) (<-chan amqp.Delivery, error) {
	//创建消费者
	msgs, err := d.mq.Channel.Consume(key, "", true, false, false, false, nil)
	if err != nil {
		global.Log.Error("Failed to declare an Consume", zap.Error(err))
		return nil, err
	}
	return msgs, nil
}

func (d *Dao) SendRabbitMqMessage(key string, sendBody []byte, Expiration int64) error {
	//获取对应的生产者
	producer := d.mq.ProducerList[key]

	if err := d.mq.Channel.Publish(producer.Exchange.Name, "", true, false, amqp.Publishing{
		ContentType: "application/json",
		Body:        sendBody,
		Expiration:  fmt.Sprintf("%d", Expiration),
	}); err != nil {
		global.Log.Error("Failed to declare an Consume", zap.Error(err))
		return err
	}
	return nil
}
